package com.company.dcm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.company.dcm.entity.Device_Ota;
import com.company.dcm.mapper.Device_OtaMapper;
import com.company.dcm.server.protocol.enums.ProtocolMessageTypeEnum;
import com.company.dcm.server.protocol.enums.ProtocolTaskOptionEnum;
import com.company.dcm.server.tcp.MessageSender;
import com.company.dcm.service.Device_otaService;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
public class Device_otaServiceImpl implements Device_otaService {
    @Autowired
    private Device_OtaMapper deviceOtaMapper;

    @Override
    public void moveDevice(String ipPort) {

        HashMap<String,Object> device = new HashMap<>();

        device.put("ip",ipPort);

        Device_Ota deviceOta = new Device_Ota();
        deviceOta.setOnlineState("离线");

        List<Device_Ota> deviceOtas = deviceOtaMapper.selectByMap(device);

        deviceOta.setId(deviceOtas.get(0).getId());

        deviceOtaMapper.updateById(deviceOta);
    }

    @Override
    public void saveDevice(String productType, String s) throws IOException {

        HashMap<String,Object> device = new HashMap<>();

        device.put("product_type","FDD" + productType);
        device.put("ip",s);

        Device_Ota deviceOta = new Device_Ota();

        deviceOta.setProductType("FDD" + productType);
        deviceOta.setIp(s);

        List<Device_Ota> deviceOtas = deviceOtaMapper.selectByMap(device);

        deviceOta.setOnlineState("在线");

        if (deviceOtas.isEmpty()){
            deviceOta.setUpgradeUponStartup(0);
            deviceOtaMapper.insert(deviceOta);
        } else if (deviceOtas.size() == 1) {
            deviceOta.setId(deviceOtas.get(0).getId());
            deviceOtaMapper.updateById(deviceOta);
        }
    }



    @Override
    public void saveDevice(String deviceCode, ChannelHandlerContext ctx) throws IOException {

        String Ip = ctx.channel().remoteAddress().toString().split(":")[0];

        HashMap<String,Object> device = new HashMap<>();

        device.put("equipment_no",deviceCode);

        Device_Ota deviceOta = new Device_Ota();

        deviceOta.setEquipmentNo(deviceCode);
        deviceOta.setOnlineState("在线");
        deviceOta.setIp(Ip);

        List<Device_Ota> deviceOtas = deviceOtaMapper.selectByMap(device);

        if (deviceOtas.isEmpty()){
            deviceOtaMapper.insert(deviceOta);
        } else if (deviceOtas.size() == 1) {

            if (deviceOtas.get(0).getUpgradeUponStartup() == 1){//设置了开机自动升级
                byte[] bytes = MessageSender.handleMessage(ProtocolMessageTypeEnum.INFORMATION,
                        ProtocolTaskOptionEnum.NOTICE_OTA_UPDATED);

                MessageSender.sendMessageByIP(Ip, bytes);
                deviceOta.setUpgradeUponStartup(0);
                deviceOta.setOnlineState("更新中");
            }
            if (deviceOtas.get(0).getOnlineState().equals("更新中")){
                deviceOta.setOnlineState("更新中");
            }

            deviceOta.setId(deviceOtas.get(0).getId());
            deviceOtaMapper.updateById(deviceOta);
        }else {
            for (Device_Ota deviceOta1 : deviceOtas){
                if (!Objects.equals(deviceOta1.getIp(), Ip)){
                    deviceOtaMapper.deleteById(deviceOta1.getId());
                }
            }
        }
    }

    @Override
    public List<Device_Ota> getList(String product_type) {
        HashMap<String,Object> device = new HashMap<>();

        device.put("product_type",product_type);

        List<Device_Ota> deviceOtas = deviceOtaMapper.selectByMap(device);

        deviceOtas.forEach(System.out::println);

        return deviceOtas;
    }

    @Override
    public boolean setUpgradeUponStartup(List<Integer> deviceIds) {

        for (Integer deviceId:deviceIds){
            Device_Ota deviceOta = new Device_Ota();

            deviceOta.setUpgradeUponStartup(1);
            deviceOta.setId(deviceId);

            try {
                deviceOtaMapper.updateById(deviceOta);
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }

        return true;
    }

    @Override
    public void updateOTA(String s) {
        Device_Ota deviceOta = new Device_Ota();

        deviceOta.setOnlineState("更新中");

        UpdateWrapper<Device_Ota> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ip",s);


        deviceOtaMapper.update(deviceOta,updateWrapper);

    }

    @Override
    public void saveDeviceByOtaUpdate(String s,ChannelHandlerContext ctx) {

        HashMap<String,Object> device = new HashMap<>();

        device.put("equipment_no",s);

        List<Device_Ota> deviceOtas = deviceOtaMapper.selectByMap(device);

        if (deviceOtas.size() == 1){
            Device_Ota deviceOta = new Device_Ota();

            deviceOta.setId(deviceOtas.get(0).getId());
            deviceOta.setOnlineState("在线");
            deviceOta.setIp(ctx.channel().remoteAddress().toString().split(":")[0]);

            deviceOtaMapper.updateById(deviceOta);
        }

    }

    @Override
    public void delectDevice(ChannelHandlerContext ctx) {

        HashMap<String,Object> device = new HashMap<>();

        device.put("ip",ctx.channel().remoteAddress().toString().split(":")[0]);

        List<Device_Ota> deviceOtas = deviceOtaMapper.selectByMap(device);

        if (deviceOtas.isEmpty()){
            return;
        }

        System.out.println(deviceOtas);

        for (Device_Ota deviceOta : deviceOtas){
            if (deviceOta.getEquipmentNo() == null){
                deviceOtaMapper.deleteById(deviceOta.getId());
            }
        }


    }
}
